<template>
  <div class="my-test">
    <white-header
      :isShowTopNav="true"
      :isShowBack="true"
      :isShowRight="true"
      :title="title"
    ></white-header>
    <div class="test-list">
      <div
        class="test-box"
        v-for="(item, index) in record_list"
        :key="'record_list' + index"
      >
        <div class="box-icon">
          <span>考试题</span>
        </div>
        <div class="test-info">
          <div class="test-name">{{ item.paperName || "未知" }}</div>
          <div class="test-time">{{ trimDate(item.createTime) }}</div>
        </div>
        <div class="tips" v-if="item.score >= item.passScore">
          <span>及格</span>
        </div>
        <div
          class="tips"
          style="background: #f99797;"
          v-if="item.score < item.passScore"
        >
          <span>不及格</span>
        </div>
      </div>
    </div>
    <div class="footer" v-if="record_list.length != 0 && record_list.length === total">
        <span class="tips">我是有底线的</span>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import WhiteHeader from "~/components/mobile/WhiteHeader.vue";
import { getExamRecord,getMyTestRecord } from "~/api/v2-question-bank/question-bank.js";
export default {
  components: {
    WhiteHeader
  },
  scrollToTop: true,
  data() {
    return {
      title: "我的考试",
      /* 考试记录 */
      record_list: [],
      total:0,
      page:1,
      pageSize:10,
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      token(state) {
        return state.token;
      },
      is_loading(state) {
        return state.is_loading;
      },
      shallowColor(state) {
        return state.web_info.THEME_COLOR + "32";
      },
      chartColor(state) {
        return state.web_info.THEME_COLOR;
      }
    })
  },
  watch: {},
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    trimDate(date) {
      if (date) {
        let _date = new Date(date);
        return `${_date.getFullYear()}-${_date.getMonth() +
          1}-${_date.getDate()}  ${_date.getHours()}:${_date.getMinutes()}`;
      } else {
        return this.$t("locale.components.mobile.GroupChat.t7");
      }
    },
    throttle(fn, delay) {
      let lastTime = 0;
      return !(function() {
        let nowTime = Date.now();
        if (nowTime - lastTime > delay) {
          fn.call(this);
          lastTime = nowTime;
        }
      })();
    },
    pullUp() {
      // 滚动条离顶部的距离
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      // 窗口可视区域高度
      let clientHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      // 滚动区域高度
      let scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      // 滚动条 + 可视区域高度 >= 滚动条高度 ==>  滚动条触底
      if (scrollTop + clientHeight >= scrollHeight) {
        if (this.total <= this.record_list.length) {
          return;
        }
        if (this.is_loading) {
          return;
        }
        this.page++;
        this.getExamRecord("pullup");
      }
    },
    eventListener() {
      this.throttle(this.pullUp, 300);
    },
    async getExamRecord(fetch_type) {
      this.SET_IS_LOADING(true);
      let params = {
        userId: this.user_info.id,
        sort:'id desc',
        offset: this.pageSize * (this.page - 1),
        limit: this.pageSize,
      };
      let result = await getMyTestRecord(params);
      // console.log("获取考试记录：", result);
      if (result && result.data && result.data.code == 200) {
        if (fetch_type == "pullup") {
          this.record_list = [...this.record_list, ...result.data.data.rows];
        } else {
          this.record_list = result.data.data.rows;
        }
        this.total = result.data.data.total;
      }
      this.SET_IS_LOADING(false);
    },
    initPage() {
      this.page = 1;
      this.getExamRecord();
    }
  },
  created() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    // this.initPage();
  },
  activated() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    window.addEventListener("scroll", this.eventListener);
    this.initPage();
  },
  deactivated() {
    if (process.client) {
      window.removeEventListener("scroll", this.eventListener);
    }
  },
  destroyed() {
    if (process.client) {
      window.removeEventListener("scroll", this.eventListener);
    }
  },
  mounted() {
    if (process.client) {
      window.addEventListener("scroll", this.eventListener);
    }
  }
};
</script>

<style lang="scss" scoped>
.my-test {
  background-color: #f7f7fa;
  min-height: 100vh;
  width: 7.2rem;
  .footer {
    padding: 0.3rem;
    font-size: 0.26rem;
    color: #babec2;
    text-align: center;
    .tips {
      position: relative;
    }
    .tips::before {
      content: "";
      display: block;
      position: absolute;
      left: -1.7rem;
      top: 50%;
      transform: translateY(-1px);
      width: 1.5rem;
      height: 1px;
      background: #babec2;
    }
    .tips::after {
      content: "";
      display: block;
      position: absolute;
      right: -1.7rem;
      top: 50%;
      width: 1.5rem;
      transform: translateY(-1px);
      height: 1px;
      background: #babec2;
    }
  }
  .test-list {
    padding: 0.288rem;
    .test-box {
      width: 6.624rem;
      height: 2.304rem;
      background-color: #ffffff;
      border-radius: 0.1536rem;
      padding: 0.288rem;
      display: flex;
      justify-content: space-between;
      align-items: center;
      position: relative;
      margin-bottom: 0.384rem;
      .tips {
        position: absolute;
        right: 0;
        bottom: 0.3rem;
        font-size: 0.2496rem;
        padding: 0.1rem 0.2rem;
        background: var(--theme-color);
        color: #ffffff;
        border-top-left-radius: 0.24384rem;
        border-bottom-left-radius: 0.24384rem;
      }
      .box-icon {
        width: 1.152rem;
        height: 1.152rem;
        border-radius: 50%;
        text-align: center;
        line-height: 1.152rem;
        background: var(--theme-color);
        font-size: 0.2304rem;
        color: #ffffff;
      }
      .box-icon-img {
        width: 1.152rem;
        height: 1.152rem;
        border-radius: 50%;
        text-align: center;
        line-height: 1.152rem;
        background: var(--theme-color);
        font-size: 0.2304rem;
        color: #ffffff;
        overflow: hidden;
        position: relative;
        img {
          display: block;
          width: 100%;
          height: 100%;
          border-radius: 50%;
          overflow: hidden;
        }
        .tips-text {
          position: absolute;
          left: 0;
          top: 0;
          width: 1.152rem;
          height: 1.152rem;
          border-radius: 50%;
          background: rgba($color: #3c434d, $alpha: 0.72);
        }
      }
      .test-info {
        width: 4.6rem;
        height: 1.152rem;
        .test-name {
          font-size: 0.288rem;
          color: #4a4b4c;
          font-weight: bold;
          margin: 0.2rem 0 0.1rem;
        }
        .test-time {
          font-size: 0.2496rem;
          color: #adb0b3;
        }
      }
    }
  }
}
</style>
